var api = require('../../utils/api.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stateList:{
      list:['文明指数'],
      index:0
    },
    cityList:{
      list:['香洲区'],
      index:0
    },
    //城市
    city:'',
    //登录个人信息
    userInfo:{
      MemberName:'',
      MemberPassword:'',
      Type:app.globalData.userInfo.Type
    }
  },
  bindViewTap(){
    wx.navigateTo({
      url: '../password/password',
    })
  },
  //登录
  login(){
    wx.showLoading({
      title: '正在登录',
    })
    if(this.data.userInfo.MemberName == '' || this.data.userInfo.MemberPassword == ''){
      wx.showToast({
        title: '账号或密码为空',
        icon: 'error',
        duration: 2000
      })
    }else{
      let data = {
        MemberName:this.data.userInfo.MemberName,
        MemberPassword:this.data.userInfo.MemberPassword,
        Type:app.globalData.userInfo.Type
      }
      //console.log(data)
      api.login(data).then( res => {
        if(res.Message == '用户名或密码错误!'){
          wx.showToast({
            title: '账号或密码错误',
            icon: 'error',
            duration: 2000
          })
        }else if(res.Message == '登录成功!'){
          //缓存账号信息  Sync（同步，同时）
          wx.setStorage({
            key:'MemberPassword',
            data:this.data.userInfo.MemberPassword
          })
          wx.setStorage({
            key:'MemberName',
            data:this.data.userInfo.MemberName
          })
           wx.switchTab({
              url: '/pages/index/index',
           })
           //保存信息为全局
           //console.log(res)
           app.globalData.userInfo.MemberID = res.Data.MemberID
           app.globalData.userInfo.MemberRealName = res.Data.MemberRealName
           app.globalData.userInfo.MemberGender = res.Data.MemberGender
           app.globalData.userInfo.MemberGroupID = res.Data.MemberGroupID
           app.globalData.userInfo.Token = res.Data.Token
           //console.log(app.globalData.userInfo)
           wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 1000
          })
        }
    }).fail(err =>{
      wx.showToast({
        title: '连接超时',
        icon: 'error',
        duration: 1000
      })
    })
    }
    
  },
  //类型选择
  changeState(e){
    //console.log(e.detail.value)
    let index = "stateList.index"
    //app.globalData.userInfo.Type = e.detail.value
    this.setData({
      [index]:e.detail.value,
    })
  },
  //选择城市
  changeCity(e){
    let index = "cityList.index"
    this.setData({
      [index]:e.detail.value,
    })
  },
  //获取登录的用户名
  userName(e){
    let name = "userInfo.MemberName"
    this.setData({
      [name]:e.detail.value
    })
  },
    //获取登录密码
    userPassword(e){
      let password = "userInfo.MemberPassword"
      this.setData({
        [password]:e.detail.value
      })
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //掩藏home按钮
    wx.hideHomeButton()
    
    if(wx.getStorageSync('MemberName') != ''){
      let name = "userInfo.MemberName"
      let password = "userInfo.MemberPassword"
      this.setData({
        [name]:wx.getStorageSync('MemberName'),
        [password]:wx.getStorageSync('MemberPassword')
    })
    }
    //console.log(wx.getStorageSync('MemberName'))
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //初始值
    this.setData({
      city:this.data.cityList.list[0]
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})